package com.weimob.mcloud.wechatwork.customer.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.msg.CustomerLoseMessageBO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.msg.CustomerLoseMessageDTO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.page.PageInfoResponse;
import com.weimob.mcloud.wechatwork.core.adapter.service.MsgAdapterService;
import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphere;
import com.weimob.mcloud.wechatwork.customer.service.LoseMessageService;
import com.weimob.mcloud.wechatwork.customer.service.user.base.BasicInfo;
import com.weimob.mcloud.wechatwork.customer.service.user.base.PageResponse;
import com.weimob.mcloud.wechatwork.customer.service.user.exception.BizException;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.msg.LoseMessageBO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.msg.LoseMessageQueryDTO;
import com.weimob.mcloud.wechatwork.customer.third.PrivateSphereThird;
import com.weimob.soa.common.response.SoaResponse;
import com.weimob.soa.common.utils.SoaUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author junlong.lv
 * @version 1.0
 * @date 2021/12/1 11:21 下午
 */
@Service
@Slf4j
public class LoseMessageServiceImpl implements LoseMessageService {

    @Autowired
    private MsgAdapterService msgAdapterService;

    @Autowired
    private PrivateSphereThird privateSphereThird;

    @Override
    public PageResponse<LoseMessageBO> queryLoseMessage(LoseMessageQueryDTO loseMessageQueryDTO) {

        BasicInfo basicInfo = loseMessageQueryDTO.getBasicInfo();

        PrivateSphere privateSphere = privateSphereThird.fetchByBosId(basicInfo.getBosId());
        if (Objects.isNull(privateSphere)) {
            throw new BizException("第三方应用或自建应用未授权，请联系管理员安装");
        }

        CustomerLoseMessageDTO customerLoseMessageDTO = new CustomerLoseMessageDTO();


        customerLoseMessageDTO.setProductInstanceId(basicInfo.getProductInstanceId());
        customerLoseMessageDTO.setBosId(basicInfo.getBosId());
        customerLoseMessageDTO.setVid(basicInfo.getVid());
        customerLoseMessageDTO.setWid(loseMessageQueryDTO.getWid());
        customerLoseMessageDTO.setPageNum(loseMessageQueryDTO.getPageNum());
        customerLoseMessageDTO.setPageSize(loseMessageQueryDTO.getPageSize());

        log.info("流失消息查询, request:{}", JSON.toJSONString(customerLoseMessageDTO));
        SoaResponse<PageInfoResponse<CustomerLoseMessageBO>, ?> pageInfoResponseSoaResponse = msgAdapterService.queryLoseMessage(customerLoseMessageDTO);
        log.info("流失提醒查询结果, request:{}, response:{}", JSON.toJSONString(customerLoseMessageDTO), JSON.toJSONString(pageInfoResponseSoaResponse));
        PageInfoResponse<CustomerLoseMessageBO> unpack = SoaUtil.unpack(pageInfoResponseSoaResponse);

        PageResponse<LoseMessageBO> pageResponse = new PageResponse<>();
        pageResponse.setPageSize(loseMessageQueryDTO.getPageSize());
        pageResponse.setPageNum(loseMessageQueryDTO.getPageNum());
        pageResponse.setTotalPage(0);
        pageResponse.setTotalCount(0L);
        pageResponse.setResults(Lists.newArrayList());

        if (Objects.isNull(unpack)) {
            return pageResponse;
        }
        pageResponse.setTotalPage(unpack.getTotalPage());
        pageResponse.setTotalCount(unpack.getTotalCount());

        List<CustomerLoseMessageBO> dataList = unpack.getDataList();
        if (CollectionUtils.isEmpty(dataList)) {
            return pageResponse;
        }


        List<LoseMessageBO> loseMessageBOS = new ArrayList<>();
        for (CustomerLoseMessageBO customerLoseMessageBO : dataList) {
            LoseMessageBO loseMessageBO = new LoseMessageBO();
            loseMessageBO.setMsgBody(customerLoseMessageBO.getMsgBody());
            loseMessageBO.setOperateTime(customerLoseMessageBO.getOperateTime());

            loseMessageBOS.add(loseMessageBO);
        }

        pageResponse.setResults(loseMessageBOS);

        return pageResponse;
    }
}
